require "user.vscode.vs_lazy_setup"

-- 复制后高亮
vim.api.nvim_create_autocmd({ "TextYankPost" }, {
  pattern = { "*" },
  callback = function()
    vim.highlight.on_yank {
      timeout = 300,
    }
  end,
})

local vscode = require "vscode-neovim"

-- local function get_filetype()
--   local bufnr = vim.api.nvim_get_current_buf()
--   local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")
--   return filetype
-- end
--
-- local function code_runner()
--   local filetype = get_filetype()
--   if filetype == "python" then
--     vscode.action "workbench.action.terminal.clear"
--     vscode.action "python.execInTerminal"
--   else
--     vscode.action "code-runner.run"
--   end
-- end

-- local opt = { noremap = true, silent = true }
-- vim.keymap.set("v", "J", ":m '>+1<CR>gv=gv", opt)
-- vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv", opt)
vim.keymap.set({ "n", "v" }, "<leader>sc", function() vscode.action "workbench.action.showCommands" end)
vim.keymap.set({ "n", "v" }, "<leader>q", function() vscode.action "workbench.action.quit" end)
vim.keymap.set({ "n", "v" }, "<leader>lv", function() vscode.action "python.setInterpreter" end)
vim.keymap.set({ "n", "v" }, "<leader>w", function() vscode.action "workbench.action.files.save" end)
vim.keymap.set({ "n", "v" }, "<leader>n", function() vscode.action "workbench.action.files.newUntitledFile" end)
vim.keymap.set({ "n", "v" }, "<leader>fw", function() vscode.action "workbench.action.experimental.quickTextSearch" end)
vim.keymap.set({ "n", "v" }, "<leader>ff", function() vscode.action "workbench.action.quickOpen" end)
vim.keymap.set({ "n", "v" }, "<leader>fe", function() vscode.action "workbench.explorer.fileView.focus" end)
vim.keymap.set({ "n", "v" }, "<leader>e", function() vscode.action "workbench.action.toggleSidebarVisibility" end)
vim.keymap.set({ "n", "v" }, "<leader>j", function() vscode.action "workbench.action.togglePanel" end)
-- vim.keymap.set({ "n", "v" }, "<leader>js", function() vscode.action "json.sort" end)
vim.keymap.set({ "n", "v" }, "<leader>th", function() vscode.action "workbench.action.terminal.toggleTerminal" end)
-- vim.keymap.set({ "n", "v" }, "<leader>e", "<cmd>call VSCodeNotify('workbench.action.toggleSidebarVisibility')<CR>")
vim.keymap.set({ "n", "v" }, "<leader>fk", function() vscode.action "workbench.action.openGlobalKeybindings" end)
vim.keymap.set({ "n", "v" }, "<leader>c", function() vscode.action "workbench.action.closeActiveEditor" end)
vim.keymap.set({ "n", "v" }, "<leader>ll", function() vscode.action "workbench.action.reloadWindow" end)

vim.keymap.set({ "n", "v" }, "<leader>fp", function() vscode.action "workbench.action.openRecent" end)
vim.keymap.set({ "n", "v" }, "<leader>la", function() vscode.action "editor.action.quickFix" end)
vim.keymap.set({ "n", "v" }, "<leader>lf", function() vscode.action "editor.action.formatDocument" end)
vim.keymap.set({ "n", "v" }, "<leader>lt", function() vscode.action "prettier.forceFormatDocument" end)
vim.keymap.set({ "n", "v" }, "<leader>vp", function() vscode.action "editor.action.marker.next" end)
vim.keymap.set({ "n", "v" }, "<leader>gg", function() vscode.action "lazygitWrapper.open" end)
vim.keymap.set({ "n", "v" }, "<leader>sp", function() vscode.action "workbench.profiles.actions.switchProfile" end)
vim.keymap.set({ "n" }, "<leader>ps", function() vscode.action "workbench.extensions.action.focusExtensionsView" end)

vim.keymap.set("n", "j", [[v:count ? 'j' : 'gj']], { noremap = true, expr = true })
vim.keymap.set("n", "k", [[v:count ? 'k' : 'gk']], { noremap = true, expr = true })

vim.api.nvim_set_keymap("n", "<A-j>", "<cmd>m .+1<cr>==", { desc = "Move down", noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<A-k>", "<cmd>m .-2<cr>==", { desc = "Move up", noremap = true, silent = true })
vim.api.nvim_set_keymap("i", "<A-j>", "<esc><cmd>m .+1<cr>==gi", { desc = "Move down", noremap = true, silent = true })
vim.api.nvim_set_keymap("i", "<A-k>", "<esc><cmd>m .-2<cr>==gi", { desc = "Move up", noremap = true, silent = true })
vim.api.nvim_set_keymap("v", "<A-j>", ":m '>+1<cr>gv=gv", { desc = "Move down", noremap = true, silent = true })
vim.api.nvim_set_keymap("v", "<A-k>", ":m '<-2<cr>gv=gv", { desc = "Move up", noremap = true, silent = true })

-- tab
vim.keymap.set({ "n", "v" }, "]b", "<Cmd>Tabnext<CR>")
vim.keymap.set({ "n", "v" }, "[b", "<Cmd>Tabprevious<CR>")

-- code-runner
-- vim.keymap.set({ "n", "v" }, "<leader>rr", function() code_runner() end)
vim.keymap.set({ "n", "v" }, "<leader>rr", function() vscode.action "code-runner.run" end)
vim.keymap.set({ "n", "v" }, "<leader>rs", function() vscode.action "code-runner.stop" end)
vim.keymap.set({ "n", "v" }, "<leader>rc", function() vscode.action "workbench.action.closePanel" end)
vim.keymap.set({ "n", "v" }, "<leader>oc", function() vscode.action "workbench.output.action.clearOutput" end)
vim.keymap.set({ "n", "v" }, "<leader>tc", function() vscode.action "workbench.action.terminal.clear" end)

-- trouble
vim.keymap.set({ "n", "v" }, "<leader>xx", function() vscode.action "workbench.actions.view.problems" end)
vim.keymap.set({ "n", "v" }, "<leader>fc", function() vscode.action "problems.action.clearFilterText" end)

-- lsp

vim.keymap.set({ "n", "v" }, "]d", function() vscode.action "editor.action.marker.nextInFiles" end)
vim.keymap.set({ "n", "v" }, "[d", function() vscode.action "editor.action.marker.prevInFiles" end)
vim.keymap.set({ "n", "v" }, "gr", function() vscode.action "editor.action.referenceSearch.trigger" end)
vim.keymap.set({ "n", "v" }, "<leader>lR", function() vscode.action "editor.action.referenceSearch.trigger" end)
vim.keymap.set({ "n", "v" }, "<leader>lr", function() vscode.action "editor.action.rename" end)
vim.keymap.set({ "n", "v" }, "<leader>lh", function() vscode.action "editor.action.triggerParameterHints" end)

-- require "user.utils.autocmd"
-- require "user.utils.keymap"
require "user.vscode.vsconf"

local opt = vim.opt
opt.ignorecase = true
opt.smartcase = true
-- -- opt.mouse:append "a"
opt.clipboard:append "unnamedplus"
opt.smartindent = true
opt.hlsearch = false
